package cq.zhcqk.util;

import org.json.JSONObject;

import java.util.Map;

/**
 * Created by zhangle on 2017/11/29.
 */
public class StringUtil {
    public static int stringToInt(String str){
        if(str == null || "".equals(str) || "null".equals(str)){
            return 0;
        }
        return (int) Double.parseDouble(String.valueOf(str));
    }

    public static String parseNull(String data){
        if(null == data || "null".equals(data)){
            return "";
        }else {
            return data;
        }
    }

    public static boolean isNull(String data){
        if(null == data || "null".equals(data) || "".equals(data)){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 判断图片路径
     * @param img
     * @return
     */
    public static String checkImgeUrl(String img){
        if(img.startsWith("http")){
            return img;
        }else{
            return Common.URL+img;
        }
    }

    public static String getFailMsg(JSONObject errorResponse){
        if(errorResponse != null){
            Map<String, Object> map = GsonUtil.GsonToMaps(errorResponse+"");
            if(stringToInt(map.get("code")+"") == 400){
                return map.get("msg")+"";
            }
        }
        return null;
    }
}
